Skip to content

FEAT: Add native Claude target and bounded tool execution - #4021

Merged
Richard Lundeen (richlundeen) merged 9 commits into
microsoft:mainfrom
richlundeen:richlundeen-claude-target-support
Nov 19, 2026
Merged

Richard Lundeen (richlundeen) merged 9 commits into
microsoft:mainfrom
richlundeen:richlundeen-claude-target-support

Conversation

@richlundeen

@richlundeen Richard Lundeen (richlundeen) commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add a public ClaudeChatTarget that uses LiteLLM's native Anthropic transport and can run executable tools. Share bounded tool execution with LiteLLMChatTarget without changing transport behavior for callers that do not use executable tools.

  • Add Claude-specific routing, credential isolation, capability discovery, and JSON-object formatting; keep explicit JSON schemas in the shared configuration and SDK adapter.
  • Run tool execution through one shared bounded loop (pyrit/prompt_target/common/tool_loop.py) used by both OpenAIResponseTarget and LiteLLMChatTarget, so the same Tool behaves the same on both: bounded rounds, direct tools and scoped providers, pydantic argument validation, linked results, ToolLoopStop metadata on every early stop, cancellation cleanup, and replayable history stored by the normalizer.
  • Include the LiteLLM extra in production Docker installs and raise its minimum version to the exercised adapter version. Limit the new Claude export smoke check to local source so independently selected PyPI wheels remain supported.
  • Preserve first-piece-only response metadata when JSON formatting replaces a response, and document the target in the Claude guide and target index.

Previously, we accessed Claude through OpenAIChatTarget and Anthropic's OpenAI-compatible endpoint. That compatibility layer squashes all system/developer messages into one top-level system prompt, hoisting them ahead of the conversation rather than preserving their original positions. It also ignores strict tool-schema enforcement and does not support OpenAI's unconstrained json_object response format. These are compatibility-layer limitations, not a lack of Claude support in PyRIT.

ClaudeChatTarget instead uses the native Anthropic Messages API through LiteLLM, with Claude-specific configuration and JSON-object formatting. The shared bounded tool loop adds local execution and stores linked tool calls/results for later turns; existing callers without executable tools retain their transport behavior. This does not promise arbitrary mid-conversation system messages or strict native JSON-schema output for every Claude model. See Anthropic's compatibility limitations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 6, 2026 22:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The Claude target can expose subscription-key credentials through persisted conversation metadata, and tools=None now causes previously accepted requests to fail.

2 open issues
What changed in this PR

Adds a native Claude target to PyRIT’s target framework and shared, bounded tool execution through LiteLLM.

Changes:

  • Introduces Claude-specific routing, credential handling, capabilities, and JSON-object formatting.
  • Adds validated tool execution with round limits, scoped providers, linked results, and replayable history.
  • Updates dependencies and Docker installs, with documentation, unit coverage, and opt-in Anthropic integration tests.
File Description
uv.lock Aligns LiteLLM minimum-version requirements.
tests/​unit/​prompt_target/​target/​test_litellm_chat_target.py Tests tool execution, limits, failures, cleanup, and replay.
tests/​unit/​prompt_target/​target/​test_claude_chat_target.py Tests Claude routing, credentials, capabilities, and adapter behavior.
tests/​unit/​infra/​test_docker_ci.py Tests Docker installation and source-specific smoke checks.
tests/​integration/​targets/​test_claude_chat_target_integration.py Adds opt-in live Claude tool and JSON tests.
pyrit/​prompt_target/​litellm_chat_target.py Implements shared bounded tool execution.
pyrit/​prompt_target/​common/​target_capabilities.py Adds known Claude capability profiles.
pyrit/​prompt_target/​common/​discover_target_capabilities.py Handles LiteLLM JSON enforcement and suppresses tools during probes.
pyrit/​prompt_target/​claude_chat_target.py Implements native Claude routing and JSON formatting.
pyrit/​prompt_target/​__init__.py Lazily exports ClaudeChatTarget.
pyproject.toml Raises the LiteLLM minimum version.
docker/​Dockerfile Installs LiteLLM and adds import smoke checks.
doc/​myst.yml Adds the Claude guide to navigation.
doc/​code/​targets/​claude_chat_target.md Documents setup, tools, JSON output, and integration testing.
doc/​code/​targets/​0_prompt_targets.md Lists the Claude target and links its guide.

Review effort: Balanced

@richlundeen

Copy link
Copy Markdown
Contributor Author

Suggestion: let a tool end the turn when its own infrastructure fails

_execute_tool_async turns every exception into a JSON error result, and the loop then asks the model to continue. That's right when the model's call is bad or its program fails. It isn't right when the tool's own infrastructure fails, e.g. a code-execution tool whose sandbox becomes unreachable, or a call whose outcome is uncertain. The model then gets an error it can't fix, and may retry or rewrite code that was fine.

Proposed change (small):

  1. Add a public ToolInfrastructureError (e.g. in pyrit/prompt_target/common/tool_provider.py, next to Tool) for tools to raise when a failure isn't the model's.

  2. In _execute_tool_async, let ToolInfrastructureError propagate instead of converting it to an {"error": ...} result.

  3. In _run_tool_loop_async, on ToolInfrastructureError:

    • answer the failing call and every remaining call in the round with a function_call_output, e.g. {"error": "tool_infrastructure_error"} (invoked=True for the call that raised, since its outcome is uncertain; invoked=False for calls not attempted);
    • append a _tool_loop_error(...) response;
    • return without requesting another completion.

    That is the same shape as the max_tool_rounds exhausted path, so all evidence is kept and the history stays valid.

  4. Tests: a provider tool that raises ToolInfrastructureError mid-round leads to no further completion request, every call id has an output, and the final message is an error response.

Context: this comes from sandboxed code execution, where a model's run_python-style tool calls run in an isolated sandbox. A sandbox outage should end the turn as an error rather than reach the model as a tool failure.

Resolve the LiteLLM test import conflict by preserving executable-tool imports and the public lazy target export.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preserve null passthrough compatibility, stop on explicit infrastructure outages with complete tool evidence, and isolate the Docker release-probe test from optional dependencies.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen

Copy link
Copy Markdown
Contributor Author

Addressed both concerns from this review in 4a66169: subscription-key credentials are excluded case-insensitively from persisted Claude identifiers, and explicit tools=None remains compatible in both targets. Added passing persistence and public-send regressions for both fixes.

@richlundeen

Copy link
Copy Markdown
Contributor Author

Implemented the infrastructure-failure behavior from this suggestion in 4a66169. Tools can raise the public ToolInfrastructureError; the LiteLLM/Claude loop then stops without another completion, preserves earlier results, answers the failing call as invoked and remaining calls as uninvoked, and returns a terminal processing error. Ordinary tool/argument errors still allow model recovery. Regressions cover first/middle/last failures for both targets, persisted call/result IDs, provider cleanup and valid replay history.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed. No blocking concerns remain. This review records source coverage; it does not authorize merge.

Integrate exact main 6053ef3. Retain the shared ToolInfrastructureError with the additive invoked contract while preserving Claude target fixes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@behnam-o Behnam (behnam-o) self-assigned this Nov 17, 2026

@behnam-o Behnam (behnam-o) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change, here are some comments from me and Claude.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Extract run_tool_loop_async and dispatch_tool_call_async into
pyrit/prompt_target/common/tool_loop.py and route both
OpenAIResponseTarget and LiteLLMChatTarget through it, so the same Tool
behaves the same on both: pydantic-only argument validation, ToolLoopStop
on every early stop, and propagation of tool and continuation failures.

Keep ClaudeChatTarget thin: lift environment variable names and credential
header redaction into LiteLLMChatTarget class attributes, pass api_key
through unchanged, and return JSON formatter output for the caller to
validate.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed. No blocking concerns remain. This review records source coverage; merging is handled separately.

@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Nov 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 19, 2026
@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Nov 19, 2026
Merged via the queue into microsoft:main with commit e40ee52 Nov 19, 2026
66 checks passed
@richlundeen
Richard Lundeen (richlundeen) deleted the richlundeen-claude-target-support branch November 19, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants